import { useStore } from "vuex";
import { useRouter } from "vue-router";
import Message from "../components/library/Message";

export default function useLoginAfter() {
  const store = useStore();
  const router = useRouter();

  // 登录成功
  const loginSuccessful = ({ result }) => {
    // 存放数据
    store.commit("user/setuse", {
      id: result.id,
      avater: result.avater,
      account: result.account,
      mobile: result.mobile,
      token: result.token,
    });

    // 成功登录跳转
    router.push("/").then(() => {
      Message({ type: "success", text: "登录成功" });
    });
  };

  // 登录失败
  const loginFailed = (error) => {
    Message({ type: "error", text: error.response.data.message });
  };

  return { loginSuccessful, loginFailed };
}
